package com.neu;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Observer;
import androidx.work.Data;
import androidx.work.OneTimeWorkRequest;
import androidx.work.WorkInfo;
import androidx.work.WorkManager;
import androidx.work.WorkRequest;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import com.neu.db.Car;
import com.neu.db.CarMode;
import com.neu.util.MyLifteCycle;
import com.neu.util.MyWork;

import java.util.List;

public class WelcomeActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcome);

        getLifecycle().addObserver(new MyLifteCycle(getIntent()));

    }

    private WorkRequest workRequest;
    private void initWork() {
        Data data=new Data.Builder()
                .putString("str","这是前面来的数据")
                .build();
        workRequest=new OneTimeWorkRequest.Builder(MyWork.class)
                .setInputData(data)
                .build();
        WorkManager.getInstance(this).enqueue(workRequest);
        WorkManager.getInstance(this).getWorkInfoByIdLiveData(workRequest.getId()).observe(this, new Observer<WorkInfo>() {
            @Override
            public void onChanged(WorkInfo workInfo) {
                switch (workInfo.getState()){
                    case SUCCEEDED:
                        Log.d("TAG", "work执行成功了 ");
                        Data data=workInfo.getOutputData();
                        Log.d("TAG", "work成功传来的数据："+data.getString("back"));
                        break;
                    case FAILED:
                        break;
                }
            }
        });
    }

    public void btnClick(View view) {
        Intent intent=new Intent(this,MainActivity.class);
        startActivity(intent);
    }

    public void btnDBClick(View view) {
        Car car=new Car("宝马",1234);
        CarMode mode=CarMode.getInstance(this);
        mode.add(car);
        List<Car> list=mode.findAll();
        for(Car c:list){
            Log.d("TAG", "btnDBClick: "+c.toString());
        }
    }

    public void btnWork(View view) {
        initWork();
    }

    public void btnMy(View view) {
        Intent intent=new Intent(this,MyActivity.class);
        startActivity(intent);
    }
}